package com.kykj.kykj.controller;



import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.service.impl.ArticlesLikeServiceImpl;
import com.kykj.kykj.vo.ArticlesLikeVo;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/articles/")
@CrossOrigin//解决跨域问题
@RestController
public class ArticlesLikeController {
    @Resource
    ArticlesLikeServiceImpl articlesLikeService;

    /**
     * 获取所有喜欢列表
     * @return resultEntity
     */
    @GetMapping("getArticlesLikeList")
    private ResultEntity getArticlesLikeList(){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articlesLikeService.getArticlesLikeList());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 增加喜欢列表
     * @param articlesLikeVo
     * @return
     */
    @PostMapping("articleslike")
    private ResultEntity addArticlesLike(@RequestBody ArticlesLikeVo articlesLikeVo){
        ResultEntity resultEntity = new ResultEntity();
        try{

            if(articlesLikeService.addArticlesLike(articlesLikeVo)>0){
                System.out.println("当前自增的id是"+articlesLikeVo.getId()+"------自增");
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！"+articlesLikeVo.getId());
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 根据id删除喜欢
     * @param id 喜欢id
     * @return resultEntity
     */
    @DeleteMapping("articleslike/{id}")
    private ResultEntity delArticlesLike(@PathVariable("id") int id){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesLikeService.delArticlesLike(id)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 修改喜欢列表
     * @param articlesLikeVo 喜欢Vo
     * @return resultEntity
     */
    @PutMapping("articleslike")
    private ResultEntity upArticlesLike(@RequestBody ArticlesLikeVo articlesLikeVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesLikeService.upArticlesLike(articlesLikeVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 根据用户id取查找喜欢列表
     * @param userId
     * @return
     */
    @GetMapping("getArticlesLikeListByUserId")
    private ResultEntity getArticlesLikeListByUserId(
            @Param("userId")int userId
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articlesLikeService.getArticlesLikeListByUserId(userId));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
}
